### get external defined data

### constant symbols

ARCH ?= arm
CROSS_COMPILE ?= arm-linux-gnueabihf-
CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar

CFLAGS := -g -O2 -Wall -Wextra -fPIC -std=c99 -D_POSIX_C_SOURCE=200112L -D_BSD_SOURCE -Iinc -I.

OBJDIR = obj
LIBDIR = lib
INCLUDES = $(wildcard inc/*.h)

### linking options

LIBS := -lsx1276 -lrt -lm -pthread

### general build targets

all: libsx1276.a libsx1276.so sx1276_rx_test sx1276_tx_test

clean:
	rm -f $(LIBDIR)/libsx1276.a
	rm -f $(LIBDIR)/libsx1276.so    
	rm -f $(OBJDIR)/*.o
	rm -rf $(OBJDIR)
	rm -rf $(LIBDIR)
    
### library module target

$(OBJDIR):
	@mkdir -p $(OBJDIR)

$(LIBDIR):
	@mkdir -p $(LIBDIR)
    
$(OBJDIR)/%.o: src/%.c $(INCLUDES) inc/config.h | $(OBJDIR)
	$(CC) -c $(CFLAGS) $< -o $@


### static library

libsx1276.a: $(OBJDIR)/radio.o $(OBJDIR)/utilities.o $(OBJDIR)/sx1276.o $(OBJDIR)/sx127x-board.o $(OBJDIR)/sx127x_timer.o $(OBJDIR)/sx127x_dio.o $(OBJDIR)/sx127x_spi.o $(OBJDIR)/spi.o $(OBJDIR)/gpio.o | $(LIBDIR)
	$(AR) rcs $(LIBDIR)/$@ $^

### dynamic library

libsx1276.so: $(OBJDIR)/radio.o $(OBJDIR)/utilities.o $(OBJDIR)/sx1276.o $(OBJDIR)/sx127x-board.o $(OBJDIR)/sx127x_timer.o $(OBJDIR)/sx127x_dio.o $(OBJDIR)/sx127x_spi.o $(OBJDIR)/spi.o $(OBJDIR)/gpio.o | $(LIBDIR)
	$(CC) -shared -fPIC -o $(LIBDIR)/$@ $^

### test programs

sx1276_rx_test: test/sx1276_rx_test.c libsx1276.a
	$(CC) $(CFLAGS) -L$(LIBDIR) $< -o $@ $(LIBS)

sx1276_tx_test: test/sx1276_tx_test.c libsx1276.a
	$(CC) $(CFLAGS) -L$(LIBDIR) $< -o $@ $(LIBS) 
 


	
### EOF
